package com.zmn.oms.admin.controller.redistribute;

import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.consts.GlobalConsts;
import com.zmn.oms.admin.controller.OmsBaseController;
import com.zmn.oms.common.constant.ConfRedistributeConsts;
import com.zmn.oms.common.dio.redistribute.RedistributeApplyAuditDIO;
import com.zmn.oms.common.dio.redistribute.RedistributeRemarkDIO;
import com.zmn.oms.common.dto.ResultDTO;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.model.bo.work.redistribute.OrderWorkRedistributeAuditBO;
import com.zmn.oms.model.bo.work.redistribute.OrderWorkRedistributeModifyBO;
import com.zmn.oms.zmn.business.interfaces.redistribute.OrderWorkRedistributeInfoBService;
import com.zmn.oms.zmn.business.interfaces.redistribute.RedistributeApplyBService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * 类描述: 工单改派操作数据 Controller类
 *
 * @author: liudi
 * @since: 2022/05/11 12:00
 */
@Controller
@RequestMapping("/work/redistribute")
@Slf4j
public class OrderWorkRedistributeModifyController extends OmsBaseController {

	@Resource
	private OrderWorkRedistributeInfoBService redistributeInfoBService;
	@Resource
	private RedistributeApplyBService redistributeApplyBService;

	/**
	 * 员工审核申请
	 *
	 * @param redistributeApplyAuditDIO 改派申请审核数据
	 * @return 是否提交审核成功
	 */
	@RequestMapping(value = "/applyAudit")
	@ResponseBody
	public ResultDTO applyAudit(@RequestBody RedistributeApplyAuditDIO redistributeApplyAuditDIO) {
		try {
			OrderWorkRedistributeAuditBO auditBo = BeanMapper.map(redistributeApplyAuditDIO, OrderWorkRedistributeAuditBO.class);
			auditBo.setAuditerId(super.getCurrentStaff().getStaffId().longValue());
			auditBo.setAuditer(super.getCurrentStaff().getRealName());
			auditBo.setAuditerType(ConfRedistributeConsts.AUDIT_TYPE_STAFF);
			auditBo.setRedistributeMasterId(redistributeApplyAuditDIO.getMasterId());
			auditBo.setRedistributeMasterName(redistributeApplyAuditDIO.getMasterName());
			auditBo.setOperatorId(super.getCurrentStaff().getStaffId().longValue());
			auditBo.setOperator(super.getCurrentStaff().getRealName());
			auditBo.setOperatorType(GlobalConsts.OPERATE_USER_TYPE_STAFF);
			auditBo.setOperateTime(DateUtil.getNow());

			// 发起审核
			redistributeApplyBService.redistributeAudit(auditBo);
			return ResultDTO.success();
		} catch (OmsBaseException omsBaseException) {
			return ResultDTO.fail(omsBaseException.getMessage());
		}
	}


	/**
	 * 添加【最新一条备注】
	 *
	 * @param redistributeRemarkDIO 改派备注输入数据
	 * @return 是否添加备注成功
	 */
	@RequestMapping(value = "/addRemark")
	@ResponseBody
	public ResultDTO addRemark(RedistributeRemarkDIO redistributeRemarkDIO) {
		try {
			OrderWorkRedistributeModifyBO modityBO = BeanMapper.map(redistributeRemarkDIO, OrderWorkRedistributeModifyBO.class);
			modityBO.setOperatorId(super.getCurrentStaff().getStaffId().longValue());
			modityBO.setOperator(super.getCurrentStaff().getRealName());
			modityBO.setOperatorType(GlobalConsts.OPERATE_USER_TYPE_STAFF);
			modityBO.setOperateTime(DateUtil.getNow());
			redistributeInfoBService.addRemark(modityBO);
		} catch (OmsBaseException omsBaseException) {
			return ResultDTO.fail(omsBaseException.getMessage());
		}
		return ResultDTO.success();
	}
}
